home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / radiance / simplerd.lha / simplerad / FinalFTP / Light / img.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-21  |  6.4 KB  |  198 lines

  1. /**********************************************************************/
  2. /* image.c                                                            */
  3. /*                                                                    */
  4. /* Mostly modifications of routines written by Paul Haeberli (1985)   */
  5. /* IRIS image storage and display                                     */
  6. /*                                                                    */
  7. /* Copyright (C) 1992, Bernard Kwok                                   */
  8. /* All rights reserved.                                               */
  9. /* Revision 1.0                                                       */
  10. /* May, 1992                                                          */
  11. /**********************************************************************/
  12. #include <stdio.h>
  13. #include <gl/gl.h>
  14. #include <device.h>
  15. #include <gl/image.h>
  16. #include <sys/sysmacros.h>
  17.  
  18. /**********************************************************************/
  19. IMAGE *image;                    /* Image */
  20. unsigned short rbuf[4096];       /* RGB buffers for reading / writing */
  21. unsigned short gbuf[4096];
  22. unsigned short bbuf[4096];
  23. unsigned char Rbuf[4096];          /* RGB buffers for display */      
  24. unsigned char Gbuf[4096];
  25. unsigned char Bbuf[4096];
  26.  
  27. /**********************************************************************/
  28. /* Prototypes                                                         */
  29. /**********************************************************************/
  30. void Short_To_Char();
  31. void Char_To_Short();
  32. void RGB_To_Gray();
  33. void Write_Image();
  34. void Show_Image();
  35. void Draw_Image();
  36. void Save_Screen_Image();
  37.  
  38. /**********************************************************************/
  39. /* Write image out in IRIS format */
  40. /**********************************************************************/
  41. void Write_Image(image_file, xsize, ysize)
  42.      char *image_file;
  43.      int xsize, ysize;
  44. {
  45.   int y;
  46.  
  47.   /* Open image file and write out a row at a time */
  48.   image = iopen(image_file, "w", RLE(1), 3, xsize, ysize, 3);
  49.   for (y = 0; y < ysize; y++) {
  50.     putrow(image, rbuf, y, 0);    /* red row */
  51.     putrow(image, gbuf, y, 1);    /* green row */
  52.     putrow(image, bbuf, y, 2);    /* blue row */
  53.   }
  54.   iclose(image);
  55. }
  56.  
  57. /**********************************************************************/
  58. /* Read in and show an IRIS image */
  59. /**********************************************************************/
  60. void Show_Image(image_file)
  61.      char *image_file;
  62. {
  63.   short val;
  64.   int xsize, ysize, zsize;
  65.  
  66.   if ((image = iopen(image_file, "r")) == NULL) {
  67.     printf("Can't open input file %s\n", image_file);
  68.     exit(1);
  69.   }
  70.   xsize = image->xsize;
  71.   ysize = image->ysize;
  72.   zsize = image->zsize;
  73.   if (xsize > (XMAXSCREEN + 1) || ysize > (YMAXSCREEN + 1)) {
  74.     printf("Input image is too large: %d %d", xsize, ysize);
  75.     exit(1);
  76.   }
  77.   prefsize(xsize, ysize);
  78.   winopen(image_file);
  79.   RGBmode();
  80.   gconfig();
  81.   RGBwritemask(0xffff, 0xffff, 0xffff);
  82.   Draw_Image(xsize, ysize);
  83.   while (1) {
  84.     if (qread(&val) == REDRAW)
  85.       Draw_Image();
  86.   }
  87. }
  88.  
  89. /**********************************************************************/
  90. /* Draw the image */
  91. /**********************************************************************/
  92. void Draw_Image(xsize, ysize, zsize)
  93.      int xsize, ysize, zsize;
  94. {
  95.   int y;
  96.  
  97.   reshapeviewport();
  98.   viewport(0, xsize - 1, 0, ysize - 1);
  99.   ortho2(-0.5, (float) xsize - 0.5, -0.5, (float) ysize - 0.5);
  100.  
  101.   for (y = 0; y < ysize; y++) {
  102.     
  103.     if (zsize < 3) {          /* Mono */
  104.       getrow(image, rbuf, y, 0); Short_To_Char(rbuf, Rbuf, xsize);
  105.       cmov2i(0, y);
  106.       writeRGB(xsize, Rbuf, Rbuf, Rbuf);
  107.  
  108.     } else {                 /* Colour */
  109.       getrow(image, rbuf, y, 0); Short_To_Char(rbuf, Rbuf, xsize);
  110.       getrow(image, gbuf, y, 1); Short_To_Char(gbuf, Gbuf, xsize);
  111.       getrow(image, bbuf, y, 2); Short_To_Char(bbuf, Bbuf, xsize);
  112.       cmov2i(0, y);
  113.       writeRGB(xsize, Rbuf, Gbuf, Bbuf);
  114.     }
  115.   }
  116. }
  117.  
  118. /**********************************************************************/
  119. /* Draw the image */
  120. /**********************************************************************/
  121. void Save_Screen_Image(xsize, ysize, zsize, image_file)
  122.      int xsize, ysize, zsize;
  123.      char *image_file;
  124. {
  125.   int x,y;
  126.   unsigned long larray[4096]; /* Buffer for reading display */
  127.   unsigned short obuf[4096];  /* Buffer for gray-scale conversion */
  128.  
  129.   if (zsize < 3) {          /* Gray */
  130.     image = iopen(image_file, "w", RLE(1), 3, xsize, ysize, 1);
  131.     image->colormap = CM_NORMAL;
  132.   } else {
  133.     image = iopen(image_file, "w", RLE(1), 3, xsize, ysize, 3);
  134.   }
  135.  
  136.   readsource(SRC_FRONT);
  137.   for (y = 1; y < ysize; y++) {
  138.     lrectread(1,y,xsize-1, y, larray);
  139.     
  140.     /* Extract RGB values */
  141.     for (x=0;x<xsize;x++) {
  142.       /* printf("l[%d] = %d\n", x, larray[x]); */
  143.       rbuf[x] = 0x0000ff & larray[x]; 
  144.       /* printf("r[%d]=%d ", x, rbuf[x]);  */
  145.       gbuf[x] = (0x00ff00 & larray[x]) >> 8 ; 
  146.       /* printf("g[%d]=%d ", x, gbuf[x]);  */
  147.       bbuf[x] = (0xff0000 & larray[x]) >> 16; 
  148.       /* printf("b[%d]=%d\n", x, bbuf[x]); */
  149.     }
  150.  
  151.     if (zsize == 3) { /* Store RGB colour */
  152.       putrow(image, rbuf, y, 0); 
  153.       putrow(image, gbuf, y, 1);
  154.       putrow(image, bbuf, y, 2);
  155.     } 
  156.     else { /* Store gray-scale */
  157.       RGB_To_Gray(rbuf,gbuf,bbuf,obuf,xsize);
  158.       putrow(image, obuf, y, 0);
  159.     }
  160.   }
  161.   iclose(image);
  162. }
  163.  
  164. /**********************************************************************/
  165. /* Change short -> unsigned char */
  166. /**********************************************************************/
  167. void Short_To_Char(sptr, bptr, n)
  168.      register unsigned short *sptr;
  169.      register unsigned char *bptr;
  170.      short n;
  171. { while (n--) *bptr++ = *sptr++; }
  172.  
  173. /**********************************************************************/
  174. /* Change unsigned char -> short */
  175. /**********************************************************************/
  176. void Char_To_Short(bptr, sptr, n)
  177.      register unsigned short *sptr;
  178.      register unsigned char *bptr;
  179.      short n;
  180. { while (n--) *sptr++ = *bptr++; }
  181.  
  182. #define R_ratio 77
  183. #define B_ratio 28
  184. #define G_ratio 151
  185. /**********************************************************************/
  186. /* "Compress" rgb to gray-scale */
  187. /**********************************************************************/
  188. void RGB_To_Gray(rbuf,gbuf,bbuf,obuf,n)
  189.      register unsigned short *rbuf, *gbuf, *bbuf, *obuf;
  190.      int n;
  191. {
  192.   int i;
  193.   
  194.   for(i=n; i--; ) 
  195.     *obuf++ = (R_ratio*(*rbuf++) + G_ratio*(*gbuf++) + B_ratio*(*bbuf++))>>8;
  196. }
  197.  
  198.